package com.cls.business.service.impl;

import com.cls.business.entity.DeclareTopic;
import com.cls.business.mapper.DeclareTopicMapper;
import com.cls.business.service.IDeclareTopicService;
import com.cls.common.entity.QueryRequest;
import com.cls.business.entity.AllotmentCapital;
import com.cls.business.mapper.AllotmentCapitalMapper;
import com.cls.business.service.IAllotmentCapitalService;
import com.cls.common.enums.DictEnum;
import com.cls.system.entity.Expert;
import com.cls.system.service.IExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.List;

/**
 * 拨付资金表 Service实现
 *
 * @author slx
 * @date 2020-11-20 09:33:12
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class AllotmentCapitalServiceImpl extends ServiceImpl<AllotmentCapitalMapper, AllotmentCapital> implements IAllotmentCapitalService {
    @Autowired
    private DeclareTopicMapper declareTopicMapper;

    @Override
    public PageInfo<AllotmentCapital> findAllotmentCapitals(QueryRequest request, AllotmentCapital allotmentCapital) {
        LambdaQueryWrapper<AllotmentCapital> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AllotmentCapital> list= this.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
    @Override
    public PageInfo<AllotmentCapital> findAllotmentCapitalsPage(QueryRequest request, AllotmentCapital allotmentCapital) {

        // 写sql
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AllotmentCapital> list= this.baseMapper.findAllotmentCapitalsPage(allotmentCapital);;
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;

    }

    @Override
    public List<AllotmentCapital> findAllotmentCapitals(AllotmentCapital allotmentCapital) {
	    LambdaQueryWrapper<AllotmentCapital> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
        if(allotmentCapital.getDeclareId()!=null){
            queryWrapper.eq(AllotmentCapital::getDeclareId,allotmentCapital.getDeclareId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }
    @Override
    public PageInfo<AllotmentCapital> findAllotmentCapitalsByDeclareId(QueryRequest request,Long declareId) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AllotmentCapital> list= this.baseMapper.findAllotmentCapitalsByDeclareId(declareId);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }
    @Override
    public AllotmentCapital selectDetailById(Long allotmentCapitalId) {
        return this.baseMapper.selectDetailById(allotmentCapitalId);
    }
    @Override
    public AllotmentCapital findNewsLog(Long declareId) {
        return this.baseMapper.findNewsLog(declareId);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createAllotmentCapital(AllotmentCapital allotmentCapital) {
        //查询课题信息
        Long declareId = allotmentCapital.getDeclareId();
        DeclareTopic declareTopic = declareTopicMapper.selectById(declareId);
        allotmentCapital.setTopicId(declareTopic.getTopicId());
        allotmentCapital.setAllocationUnitName(DictEnum.UNIT_NAME.getName());
        this.save(allotmentCapital);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAllotmentCapital(AllotmentCapital allotmentCapital) {
        this.saveOrUpdate(allotmentCapital);
    }


    /**
    * 删除
    * @param ids
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAllotmentCapitals(String[] ids) {
        List< String > list = Arrays.asList(ids);
        this.removeByIds(list);
     }

}
